package com.luoqian.demo03;

import java.lang.reflect.Field;

public class Filed_and_DeclareFiled {

    public static void main(String[] args) throws Exception {
        Class<User> clazz = User.class;
        User user = clazz.newInstance();
        Field[] fields = clazz.getFields(); // 获取类型中定义的字段 公有public的字段 以及弗雷中共有的字段
        for (Field field:fields) {
            System.out.println(field.getModifiers()  + "--" + field.getName());
        }

        System.out.println("------------------------");
        // 可以获取私有字段 只能够获取当前类中
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field:declaredFields){
            System.out.println(field.getModifiers()  + "--" + field.getName());
        }

        //获取name字段对应的Field
        Field nameField = clazz.getDeclaredField("idCard");
        nameField.setAccessible(true);
        nameField.set(user,"xxxx22");
        System.out.println(user.getIdCard());
        // 如果对静态属性赋值
        Field salaryField = clazz.getDeclaredField("salary");
        salaryField.set(null,"hu nan chang sha ");
        System.out.println(User.salary);

    }
}
